﻿using UnitDriven;
using System;
using System.ComponentModel;
using System.Threading;
#if NETFX_CORE
using System.Threading.Tasks;
#endif

namespace Silverlight
{
    [TestClass]
    public class TimeoutTests : TestBase
    {
        [TestMethod]
        public void TestSetTimeout()
        {
            var context = GetContext();
            context.SetTimeout(TimeSpan.FromSeconds(1));
        }

        [TestMethod]
        public void TestTimeout()
        {
#if NETFX_CORE
            Task.Delay(TimeSpan.FromSeconds(5)).Wait();
#else
			Thread.Sleep(TimeSpan.FromSeconds(5));
#endif
        }

		[TestMethod]
		public void AsyncTimeoutExample()
		{
			using (var context = GetContext())
			{
				context.SetTimeout(TimeSpan.FromSeconds(2));
				BackgroundWorker worker = new BackgroundWorker();
				worker.DoWork += (o1, e1) =>
				{
#if NETFX_CORE
                    Task.Delay(TimeSpan.FromSeconds(5)).Wait();
#else
					Thread.Sleep(TimeSpan.FromSeconds(5));
#endif
				};

				worker.RunWorkerCompleted += (o2, e2) =>
				{
					context.Assert.Fail("If you see this in the results, the timeout did not work correctly!");
				};

				worker.RunWorkerAsync();
			}
		}
    }
}
